package com.cj.common.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class FileUtil {
	protected final static Logger logger = LoggerFactory
			.getLogger(FileUtil.class);

	/**
	 * 【】
	 * 
	 * @param path
	 *            相对路径
	 * @param saveFilename
	 * @param file
	 * @throws Exception
	 */
	public static void createFile(String path, String saveFilename, File file)
			throws Exception {
		FileOutputStream outputStream;
		try {
			File saveFile = new File(AppInfo.getAppPath() + path + "/"
					+ saveFilename);
			if (file == null) {
				throw new NullPointerException("没有需要保存的文件！not found the save file!");
			}
			File parent = saveFile.getParentFile();
			if (!parent.exists()) {
				parent.mkdirs();
			}

			/* 指定保存的文件夹为SysConfig中定义的文件夹 */
			outputStream = new FileOutputStream(saveFile);
			FileInputStream fileIn = new FileInputStream(file);
			byte[] buffer = new byte[2048];
			int len;
			while ((len = fileIn.read(buffer)) > 0) {
				outputStream.write(buffer, 0, len);
			}
			fileIn.close();
			outputStream.close();
		} catch (FileNotFoundException e) {
			logger.error("保存csv文件错误！", e);
			throw new Exception("File Not Found");
		} catch (IOException e) {
			logger.error("保存csv文件错误！", e);
			throw new Exception("File read Exception");
		}
	}
}
